package io.renren.modules.doc.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.doc.entity.DenseRagEntity;
import io.renren.modules.doc.service.DenseRagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 向量检索控制器
 */
@RestController
@RequestMapping("doc/vector")
public class DenseRagController {
    @Autowired
    private DenseRagService denseRagService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("doc:vector:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = denseRagService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("doc:vector:info")
    public R info(@PathVariable("id") Long id){
        DenseRagEntity denseRag = denseRagService.getById(id);

        return R.ok().put("denseRag", denseRag);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("doc:vector:save")
    public R save(@RequestBody DenseRagEntity denseRag){
        // 如果没有提供向量嵌入，则自动生成
        if (denseRag.getEmbeddings() == null) {
            String text = denseRag.getTitle() + " " + denseRag.getContent();
            byte[] embeddings = denseRagService.generateEmbeddings(text);
            denseRag.setEmbeddings(embeddings);
        }
        
        denseRagService.save(denseRag);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("doc:vector:update")
    public R update(@RequestBody DenseRagEntity denseRag){
        // 如果内容发生变化，重新生成向量嵌入
        DenseRagEntity oldEntity = denseRagService.getById(denseRag.getId());
        if (oldEntity != null && 
            (!oldEntity.getTitle().equals(denseRag.getTitle()) || 
             !oldEntity.getContent().equals(denseRag.getContent()))) {
            String text = denseRag.getTitle() + " " + denseRag.getContent();
            byte[] embeddings = denseRagService.generateEmbeddings(text);
            denseRag.setEmbeddings(embeddings);
        }
        
        denseRagService.updateById(denseRag);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("doc:vector:delete")
    public R delete(@RequestBody Long[] ids){
        denseRagService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    
    /**
     * 向量相似度检索
     */
    @GetMapping("/search")
    @RequiresPermissions("doc:vector:search")
    public R search(@RequestParam String query, @RequestParam(defaultValue = "10") int limit) {
        List<DenseRagEntity> results = denseRagService.searchSimilar(query, limit);
        
        return R.ok().put("list", results);
    }
}
